<?php
namespace app\wxapp\controller;

use app\model\Record; 
use app\model\Theme; 
use app\model\Fans; 

class MotherBoxes extends Base
{
    public function Mother_boxes()
    {
global $_P, $_G;
$fans = $this->checkuser();
$system=$_G['system'];
$themeid = intval($_P['themeid']);
$theme = Theme::getThemeByIdxcx($themeid);
$config = Theme::getConfig($themeid);
if(empty($theme['id'])){
    wxapp_result(1, '活动入口不正确');
    exit;
}
$op = !empty($_P['op']) ? $_P['op'] : 'display';

//获取定单信息：theMyRecord
//if ($config['maxcount']>0) {
//    $recordid = intval($_P['recordid']);
//}
//
//extract(Record::getTheMyRecord($fans,$recordid,$config,$themeid));
//extract(Record::getShowRecord($recordid,$config,$themeid));
//if (Record::isFreeJoin($config,$themeid) && $config['touxiang']==2) {
//    $config['touxiang']=1;
//}

if ($op=='menu'){
    $mother_info = array();
    $bottombut = array();
    if ($system['is_mother_boxes']){
        $list = self::getFansList($system,$theme,$config,$fans,$themeid);
        $mother_info['need_num'] = max(0,$config['mother_boxes_renshu']  - $list['count']);
        $mother_info['fanslist'] = array_chunk($list['fanslist'],5);;
        $mother_info['nowlist'] = array_chunk($list['nowlist'],5);;
        $mother_info['friendlist'] = Record::getRecordFromuid($themeid,$_G['member']['uid']);
        $mother_info['friendlist_count'] = count($mother_info['friendlist']);
        if ($mother_info['friendlist_count']<$config['mother_boxes_friend']){
            for ($x=$mother_info['friendlist_count']; $x<$config['mother_boxes_friend']; $x++) {
                $mother_info['friendlist'][$x]=array(
                    'avatar'=>"https://css2020.yixuan.net/2020css/yx2020/images/plus3.png",
                    'username'=>"",
                );
            }
        }
        $mother_config = array(
            'mother_menu'=>false,
            'mother_boxes_limit'=>$config['mother_boxes_limit'],
            'mother_boxes_renshu'=>$config['mother_boxes_renshu'],
            'mother_boxes_xuni'=>$config['mother_boxes_xuni'],
            'mother_boxes_top'=>$config['mother_boxes_top'],
            'mother_boxes_ani'=>$config['mother_boxes_ani'],
            'mother_boxes_form_select'=>$config['mother_boxes_form_select'],
            'boxes_img'=>'https://css2020.yixuan.net/2020css/mother_boxes/images/mother_top_icon.png',
        );
    }else{
        $mother_info=array();
        $mother_config=array(
            'mother_menu'=>true,
            'boxes_img'=>'https://css2020.yixuan.net/2020css/mother_boxes/images/mother_top_icon.png',
        );
    }
    $result = array(
        'mother_info'=>$mother_info,
        'mother_config'=>$mother_config,
    );
    wxapp_result(0, '',$result);
}

}function getFanslistMotherBoxes($themeid,$touxiang,$limit=20,$showalldata,$tiaojian = -1){
    global $_G;
    if($themeid <= 0){
        return false;
    }
    if ($limit==0){
        $limit=20;
    }
    $params = array();
    $tiaojian_sql="";
    if ($tiaojian != -1){
        if ($tiaojian==0){
            $tiaojian_sql = " and addtime>=".strtotime(date('Y-m-d 00:00:00'));
        }elseif ($tiaojian==1){
            $tiaojian_sql = " and addtime >=". (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600);
        }elseif ($tiaojian==2) {
            $tiaojian_sql = " and addtime >=" . strtotime(date('Y-m-01 00:00:00'));
        }elseif ($tiaojian==10){
            $tiaojian_sql = " and addtime <".strtotime(date('Y-m-d 00:00:00'));
        }elseif ($tiaojian==11){
            $tiaojian_sql = " and addtime <". (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600);
        }elseif ($tiaojian==12) {
            $tiaojian_sql = " and addtime <" . strtotime(date('Y-m-01 00:00:00'));
        }
    }


    if ($showalldata==1) {
        if ($touxiang == 1) {
            //资料
            $sql = "SELECT username,avatar FROM " . tablename(t_fans) . "
                            WHERE username<>'' {$tiaojian_sql} order by id desc limit " . $limit;
        } elseif ($touxiang == 2) {
            //支付人数
            $sql = "SELECT username,avatar FROM " . tablename(t_record) . "
                            WHERE  (status > 0 or status =-2)  {$tiaojian_sql}  order by id desc limit " . $limit;
        } else {
            //查看
            $sql = "SELECT nickname as username,avatar FROM " . tablename(t_fans) . " where avatar<>'' and nickname<>'新用户' and nickname<>'微信用户' and nickname!='不在活动地区' {$tiaojian_sql} 
                             order by id desc limit " . $limit;
        }
    }elseif ($showalldata==2) {
        $params[':themeid'] = $themeid;
        $sql = "SELECT username,avatar FROM ".tablename(t_record)."
                            WHERE themeid =:themeid and status =-2  {$tiaojian_sql}  order by id desc limit ".$limit;
    }else{
        $params[':themeid'] = $themeid;
        if ($touxiang==1){
            //资料
            $sql = "SELECT username,avatar FROM ".tablename(t_fans)."
                            WHERE themeid =:themeid and username<>'' {$tiaojian_sql}  order by id desc limit ".$limit;
        }elseif ($touxiang==2){
            //支付人数
            $sql = "SELECT username,avatar FROM ".tablename(t_record)."
                            WHERE themeid =:themeid and (status > 0 or status =-2)  {$tiaojian_sql}  order by id desc limit ".$limit;
        }elseif ($touxiang==3){
            //支付人数
            $sql = "SELECT nickname as username,avatar FROM ".tablename(t_record)."
                            WHERE themeid =:themeid and (status > 0 or status =-2)  {$tiaojian_sql}  order by id desc limit ".$limit;
        }else{
            //查看
            $sql = "SELECT nickname as username,avatar FROM ".tablename(t_fans)."
                            WHERE themeid =:themeid and avatar<>'' and nickname<>'新用户' and nickname<>'微信用户' and nickname!='不在活动地区' {$tiaojian_sql}  order by id desc limit ".$limit;
        }
    }
    $fanslist = db_select($sql,$params);
    return $fanslist;
}

function getFansList($system,$theme,$config,$fans,$themeid)
{
    global $_G;
    $txnum = $config['mother_boxes_renshu'];
    $endday =  $system['endday']*24*3600;
    //参加人数
    if (!(($fans['status'] == 2 || ($theme['endtime'] + $endday) < TIMESTAMP) && $system['s']['istouxiang'] == 1)) {
        $fanslist = self::getFanslistMotherBoxes($themeid, $config['touxiang'], $txnum, $config['showalldata'],0);
    } else {
        $fanslist = array();
    }
    //获取当前时间是否有虚拟数；
    if ($config['mother_boxes_endtime']>0){
        if ($config['mother_boxes_endtime']<=5){
            $rand_max = intval((180/21) *  (date('d')%3+1)*(date('w')+1));
        }else{
            $rand_max = intval((240/28) *  (date('d')%4+1)*(date('w')+1));
        }
        switch($config['mother_boxes_endtime']){
            case 1:
                $max = 6*60 + $rand_max;break;
            case 2:
                $max = 9*60 + $rand_max;break;
            case 3:
                $max = 12*60 + $rand_max;break;
            case 4:
                $max = 15*60 + $rand_max;break;
            case 5:
                $max = 18*60 + $rand_max;break;
            case 6:
                $max = 6*60 + $rand_max;break;
            case 7:
                $max = 10*60 + $rand_max;break;
            case 8:
                $max = 14*60 + $rand_max;break;
            case 9:
                $max = 18*60 + $rand_max;break;
            default:
                $max = 0;
                break;
        }
        if ($max>0){
//            echo '$max:'.date("Y-m-d H:i:s",$max*60-28800)."<br>";
            $max = ($max-360)*60; //获得结果时间并减去6点前的时间；
            $step = intval($max / $config['mother_boxes_renshu']); //计算出每一个成交用时
            $time = TIMESTAMP - strtotime(date("Y-m-d 06:00:00"));  //获取当前间
            $now = max(0,intval($time/$step)); //得到当前时间应成交数
            $now = $now >$config['mother_boxes_renshu']?$config['mother_boxes_renshu']:$now; //如果虚拟数大于限购数，则等于限购数;
            $xuni = max(0,$now- count($fanslist));
        }
    }else{
        $xunifans = self::getFanslistMotherBoxes($themeid, $config['touxiang'], $txnum, 2,0);
        $xuni = $config['mother_boxes_xuni']-count($xunifans);
        $xuni = max(0,$xuni);
    }
    //虚拟头像
    if (count($fanslist) < $txnum && $xuni > 0) {
        $tempcount = $txnum - count($fanslist);
        if ($tempcount > $xuni) {
            $tempcount = $xuni;
        }

        $kaishishu = date(m)*5+date(d)*30+count($fanslist)+intval( substr(MODULE_NAME, -2))*2+1000;

        $xunisql = "SELECT nickname , username,avatar FROM " . tablename(t_2019fans) . " where
                     id >" . $kaishishu . " limit " . $tempcount;

        $xunilist = db_select($xunisql);

        foreach ($xunilist as $index =>  $row){
            $date=$row;
            $date['uniacid']  = $_G['mpid'];
            $date['themeid']  = $themeid;
            $date['uid']=-1;
            $date['status']=-2;
            $date['addtime']=TIMESTAMP;
            $date['uncode'] = $themeid.'-'.TIMESTAMP.rand(1000,1000000);
            $insertid = db_insert(t_record, $date);

            if ($config['touxiang'] == 0) {
                $xunilist['username']=$row['nickname'];
            }
        }
        $fanslist = array_merge($fanslist, $xunilist);
    }

    $fanslist_now = $fanslist;
    if (!(($fans['status'] == 2 || ($theme['endtime'] + $endday) < TIMESTAMP) && $system['s']['istouxiang'] == 1)) {
        $txnum = 60;
        $fanslist = Record::getFanslistByThemeId($themeid, $config['touxiang'], $txnum, $config['showalldata'],10);
        if (count($fanslist)<$config['fakejoin']){
            $tempcount = $config['fakejoin']-count($fanslist);
            $xunilist2 = Fans::getXuniFans($config,$tempcount,$config['touxiang'],0);
            $fanslist = array_merge($fanslist , $xunilist2);
        }
    }

    if (!empty($fanslist_now)){
        foreach ($fanslist_now as $index => $row) {
            if ($config['touxiang'] > 0 && $config['isshowname'] == 1) {
                $fanslist_now[$index]['username']  = substr_cut_username($row['username']);
            }
            if ($config['mother_boxes_today_show']==0){
                $fanslist_now[$index]['istoday']=true;
            }else{
                $fanslist_now[$index]['istoday']=false;
            }
        }
    }
    if (!empty($fanslist)){
        foreach ($fanslist as $index => $row) {
            if ($config['touxiang'] > 0 && $config['isshowname'] == 1) {
                $fanslist[$index]['username']  = substr_cut_username($row['username']);
            }
            $fanslist[$index]['istoday']=false;
        }
    }

    if ($config['mother_boxes_today_show']!=0){
        $fanslist = array_merge($fanslist_now,$fanslist);
        $fanslist_now=array();
    }
    if (count($fanslist )>20) {
        $fanszs = count($fanslist);
        $fanszs = $fanszs - ($fanszs %5);
        $fanslist = array_slice($fanslist,0,$fanszs);
    }
    return array(
        'fanslist'=>$fanslist,
        'nowlist'=>$fanslist_now,
        'count'=>count($fanslist_now),
    );;
}
}